package com.dormitory_springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory_springboot.bean.Dormitory;
import com.dormitory_springboot.form.FormInline;
import com.dormitory_springboot.form.MoneyFrom;
import com.dormitory_springboot.form.NewForm;
import com.dormitory_springboot.service.DormitoryService;
import com.dormitory_springboot.vo.Resultdor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @auther 夏杨
 **/
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    DormitoryService dormitoryService;
    @GetMapping("/getdormitory")
    public Page getdormitory(FormInline formInline){
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(true, "dorid",formInline.getBuilding()+"#"+formInline.getFloor());
        Page<Dormitory> page = new Page<>(formInline.getPage(),formInline.getPagesize());
        return dormitoryService.page(page,queryWrapper);
    }
    @GetMapping("/getAllDormitory")
    public List<Dormitory> getAllDormitory(){
        return dormitoryService.list();
    }
    @GetMapping("/deleteOneDor/{building}/{room}")
    public void deleteOneDor(@PathVariable("building") String building, @PathVariable("room") String room){
        String str = building + "#" +room;
        dormitoryService.removeById(str);
    }
    @GetMapping("/getDormitoryById/{building}/{room}")
    public Dormitory getDormitoryById(@PathVariable("building") String building,@PathVariable("room") String room){
        String id = building +"#"+ room;
        return dormitoryService.getById(id);
    }
    @GetMapping("/editdormitory")
    public void editdormitory(NewForm newForm){
        String id = newForm.getBuilding() +"#"+ newForm.getRoom();
        String newid = newForm.getNewBuilding() +"#"+ newForm.getNewRoom();
        UpdateWrapper<Dormitory> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("dorid",id).set("dorid",newid);
        queryWrapper.set("building",newForm.getNewBuilding());
        dormitoryService.update(queryWrapper);
    }
    @GetMapping("/getUseableDormitory/{sex}")
    public Resultdor getUseableDormitory(@PathVariable("sex") String sex){
        QueryWrapper queryWrapper = new QueryWrapper();
        Resultdor resultdor = new Resultdor();
        int all = 0;
        if(sex.equals("男")){
            queryWrapper.likeRight(true,"Dorid","2").lt("population",6);
        }else queryWrapper.likeRight(true,"Dorid","1").lt("population",6);
        List<Dormitory> list = dormitoryService.list(queryWrapper);
        for (Dormitory dormitory : list) {
            all += 6-dormitory.getPopulation();
        }
        resultdor.setTotal(all);
        resultdor.setData(list);
        return resultdor;
    }
    @PostMapping("/reloadPopulationById")
    public void reloadPopulationById(@RequestBody List<Dormitory> list){
        UpdateWrapper<Dormitory> updateWrapper = new UpdateWrapper<>();
        list.forEach(dormitory -> {
            updateWrapper.eq("dorid",dormitory.getDorid()).set("population",dormitory.getPopulation());
            dormitoryService.update(updateWrapper);
        });
    }
    @GetMapping("/getEmptyDorid/{sex}")
    public List<String> getEmptyDorid(@PathVariable("sex") String sex){
        List<String> dorids = new ArrayList<>();
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        if(sex.equals("男"))
            queryWrapper.likeRight("dorid",2).lt("population",6);
        else
            queryWrapper.likeRight("dorid",1).lt("population",6);
        List<Dormitory> d = dormitoryService.list(queryWrapper);
        d.forEach(dormitory -> {
            dorids.add(dormitory.getDorid());
        });
        return dorids;
    }
    @GetMapping("/delete1PoAtDor/{building}/{room}")
    public void delete1PoAtDor(@PathVariable("building") String building,@PathVariable("room") String room){
        String  dorid = building + "#" +room;
        Dormitory dormitory = dormitoryService.getById(dorid);
        UpdateWrapper<Dormitory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dorid",dorid).set("population",dormitory.getPopulation()-1);
        dormitoryService.update(updateWrapper);
    }
    @GetMapping("/add1PoAtDor/{building}/{room}")
    public void add1PoAtDor(@PathVariable("building") String building,@PathVariable("room") String room){
        String  dorid = building + "#" +room;
        Dormitory dormitory = dormitoryService.getById(dorid);
        UpdateWrapper<Dormitory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dorid",dorid).set("population",dormitory.getPopulation()+1);
        dormitoryService.update(updateWrapper);
    }
    @GetMapping("/editPoAtDor/{building}/{room}")
    public void editPoAtDor(@PathVariable("building") String building,@PathVariable("room") String room,@RequestParam("num")Integer num){
        String  dorid = building + "#" +room;
        UpdateWrapper<Dormitory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dorid",dorid).set("population",num);
        dormitoryService.update(updateWrapper);
    }
    @GetMapping("/getEchartsSIdata/{sex}")
    public List<Map<String,Object>> getEchartsSIdata(@PathVariable("sex")String sex){
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        if(sex.equals("男")){
            queryWrapper1.likeRight("dorid","2");
            queryWrapper2.likeRight("dorid","2");
        }else {
            queryWrapper1.likeRight("dorid","1");
            queryWrapper2.likeRight("dorid","1");
        }
        queryWrapper1.lt("population",6);
        queryWrapper2.eq("population",6);
        List<Dormitory> hasEmpty = dormitoryService.list(queryWrapper1);
        List<Dormitory> noEmpty = dormitoryService.list(queryWrapper2);
        AtomicLong empty = new AtomicLong(0L);
        AtomicLong pople = new AtomicLong(0L);
        hasEmpty.forEach(dormitory -> {
            pople.addAndGet(dormitory.getPopulation());
            empty.addAndGet(6 - dormitory.getPopulation());
        });
        pople.addAndGet(noEmpty.size()*6);
        Map<String,Object> mapP = new HashMap<>();
        Map<String,Object> mapE = new HashMap<>();
        mapP.put("value",pople);
        mapP.put("name","已入住人数");
        mapE.put("value",empty);
        mapE.put("name","剩余空位");
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(mapE);
        list.add(mapP);
        return list;
    }
    @GetMapping("/rechargeBills/{building}/{room}")
    public void rechargeBills(@PathVariable("building")String building, @PathVariable("room")String room, MoneyFrom moneyFrom){
        UpdateWrapper<Dormitory> updateWrapper = new UpdateWrapper<>();
        String dorid = building + "#" + room;
        double balance;
        updateWrapper.eq("dorid",dorid);
        Dormitory dormitory = dormitoryService.getById(dorid);
        if(moneyFrom.getType() == 1){
            balance = dormitory.getBilllight() + moneyFrom.getMoney();
            updateWrapper.set("billlight",balance);
        }else {
            balance = dormitory.getBillaircon() + moneyFrom.getMoney();
            updateWrapper.set("billaircon",balance);
        }
        dormitoryService.update(updateWrapper);
    }
    @GetMapping("/countAllSpace")
    public List<Dormitory> countAllSpace(@RequestParam("sex")String sex){
        QueryWrapper qw = new QueryWrapper();
        if ("男".equals(sex)){
            qw.likeRight("dorid","2");
        }else qw.likeRight("dorid","1");
        return dormitoryService.list(qw);
    }
}
